/****************************************************************************
	
	Date: October, 2017
	Updated: March 2022 => Do year by year to save on memory
	
	Program: Model Calibration
	
	This file:
	Merges the Firm Level and WIOD Tables and then calculates the three key
	parameters and produces figures.
	
	Data to put in $datapath: 
	- Firm_Level_$sample from 02_0_Firm_Level.do
	- WIOT_Calibration_Wide from 03_0_WIOT.do
	- temp___ from 02_0_Firm_Level.do
	- bilateralexports_wide from 02_0_Firm_Level.do
	
	Output:
	- Firm_Level_France_pi_alpha_gamma_$method
	- Firm_Level_France_alpha_$method
	- Firm_Level_France_pi_$method
	- Firm_Level_France_gamma_COUNTRY_$method
	- difference_$method
	
****************************************************************************/

cd "$statafiles"
sysdir set PLUS"`c(pwd)'"
*ssc install egenmore
cd "$datapath"
clear
clear mata
clear matrix
set maxvar 50000

forvalues y=1996(1)2008{
	use $outputpath/bilateralexports_wide_${sample}, clear
	keep if year==`y'
	save $outputpath/bilateralexports_wide_${sample}_`y', replace
}
erase $outputpath/bilateralexports_wide_${sample}.dta
forvalues y=1996(1)2008{
*** +++ Construct wide import dataset

use temp___, clear
keep if year==`y'
drop iso3 wiot12_jp lag_import
compress
greshape wide import, i(siren year) j(iso3wiotsec) string
drop *E
gsort siren year
local vars

compress
*saveold bilateralimports_wide, replace version(13)
save bilateralimports_wide_${sample}_`y', replace 

*** +++ Calculate Alpha and Raw Input Variables and winsorize Alpha +++ ***

use Firm_Level_${sample}, clear
keep if year==`y'
g labor_share=vabcf/catotal
replace labor_share=. if labor_share<=0 | labor_share>1
drop if labor_share==.

//bys year: sum labor_share
//bys year wiot12_jp: sum labor_share
//pwcorr labor_share vabcf, sig
egen sect=group(wiot12_jp)
g lvabcf=log(vabcf)
g logit_labor_share=log(labor_share/(1-labor_share))
gen firm_inputs = achamar+achampr
drop if firm_inputs==. | firm_inputs<=0 | wiot12_jp==""
gen import_share=firm_import/firm_inputs
merge m:1 wiot12_jp year using WIOT_alpha
keep if _merge==3
bys wiot12_jp year: egen ind_sales=sum(catotal)
gen weight = catotal/ind_sales
gen weighted_alpha=weight*labor_share
bys wiot12_jp year: egen mean_alpha=sum(weighted_alpha)
gen shift = alpha_wiot-mean_alpha
gen labor_share_2 = labor_share+shift
bys wiot12_jp year: egen fitted_mean = sum(weight*labor_share_2)
replace labor_share_2=0 if labor_share_2<0
replace labor_share_2=1 if labor_share_2>1
g check = labor_share_2
foreach i in $wiotsector_High_Merch {
	replace check = . if wiot12_jp=="`i'"
}
count if check!=.
count if check!=. & (check<0|check>1)
drop check
bys wiot12_jp year: egen constrained_mean = sum(weight*labor_share_2)
//bys wiot12_jp year: sum  alpha_wiot mean_alpha fitted_mean constrained_mean
bys wiot12_jp year: gen dif = fitted_mean - constrained_mean
//sum dif
g labelWIOT="Food, Beverages, Tobacco" if wiot12_jp=="15t16"
replace labelWIOT="Textile Products" if wiot12_jp=="17t18"
replace labelWIOT="Leather, Footwear" if wiot12_jp=="19"
replace labelWIOT="Wood Products" if wiot12_jp=="20"
replace labelWIOT="Pulp, Paper, Publishing" if wiot12_jp=="21t22"
replace labelWIOT="Coke, Refined Petroleum, Nuclear Fuel" if wiot12_jp=="23"
replace labelWIOT="Chemical Products" if wiot12_jp=="24"
replace labelWIOT="Rubber and Plastics" if wiot12_jp=="25"
replace labelWIOT="Other Non-Metallic Minerals" if wiot12_jp=="26"
replace labelWIOT="Basic and Fabricated Metals" if wiot12_jp=="27t28"
replace labelWIOT="Machinery n.e.c." if wiot12_jp=="29"
replace labelWIOT="Electrical, Optical Equipment" if wiot12_jp=="30t33"
replace labelWIOT="Transport Equipment" if wiot12_jp=="34t35"
replace labelWIOT="Manufacturing n.e.c." if wiot12_jp=="36t37"
replace labelWIOT="Agriculture, Hunting, Forestry, Fishing" if wiot12_jp=="AtB"
replace labelWIOT="Mining, Quarrying" if wiot12_jp=="C"
replace labelWIOT="Electricity, Gas, Water Supply" if wiot12_jp=="E"
replace labelWIOT="Construction" if wiot12_jp=="F"
replace labelWIOT="Wholesale and Retail Motor Vehicles and Fuel" if wiot12_jp=="50"
replace labelWIOT="Wholesale Trade" if wiot12_jp=="51"
replace labelWIOT="Retail Trade" if wiot12_jp=="52"
replace labelWIOT="Hotels and restaurants" if wiot12_jp=="H"
replace labelWIOT="Inland Transport" if wiot12_jp=="60"
replace labelWIOT="Water Transport" if wiot12_jp=="61"
replace labelWIOT="Air Transport" if wiot12_jp=="62"
replace labelWIOT="Other Transport Activities" if wiot12_jp=="63"
replace labelWIOT="Post and Telecommunications" if wiot12_jp=="64"
replace labelWIOT="Real Estate" if wiot12_jp=="70"
replace labelWIOT="Business Activities" if wiot12_jp=="71t74"
replace labelWIOT="Public Administration" if wiot12_jp=="L"
replace labelWIOT="Education" if wiot12_jp=="M"
replace labelWIOT="Health and Social Work" if wiot12_jp=="N"
replace labelWIOT="Other Personal Services" if wiot12_jp=="O"
gen labor_share_original = labor_share
drop ind_sales weight weighted_alpha mean_alpha shift alpha_wiot country fitted_mean constrained_mean labor_share dif labelWIOT _merge
rename labor_share_2 labor_share
foreach i in $wiotsector_High_Merch {
	replace labor_share = labor_share_original if wiot12_jp=="`i'"
}
save Calibration_Hold_${sample}_`y'.dta, replace

keep siren year wiot12_jp
save temp_ref_${sample}_`y', replace


*** +++ Calculate Heterogeneous Gamma Method 2 +++ ***

use bilateralimports_wide_${sample}_`y', clear
*gsort siren year wiot12_jp
merge 1:1 siren year using temp_ref_${sample}_`y', nolabel
drop if _merge!=3
drop _merge
erase temp_ref_${sample}_`y'.dta
merge 1:1 siren year wiot12_jp using Calibration_Hold_${sample}_`y'.dta, nolabel
drop _merge
merge m:1 wiot12_jp year using $wiotdata/WIOT_Calibration_Wide, nolabel
drop if _merge!=3
drop _merge
count
count if (import_share>1 & import_share!=.)
replace import_share=0 if import_share<0 & import_share!=.
replace import_share=1 if import_share>1 & import_share!=.
replace firm_import=0 if import_share==0
replace firm_inputs=firm_import if import_share==1

preserve
foreach i in $wiotcountry {
	foreach j in $wiotsector {
		capture: replace import`i'`j'=0 if import`i'`j'==.
		gen gamma_`i'`j' =.
		capture: replace gamma_`i'`j' = import`i'`j' / firm_inputs
		replace gamma_`i'`j'=0 if gamma_`i'`j'==. | import_share==0
	}
}
foreach i in $wiotcountry {
	foreach j in $wiotsector_NT_Only {
		gen gamma_`i'`j'=0
	}
}
gen FRA_Residual=1
bysort year wiot12_jp: egen summed_inputs`i'`j' = sum(firm_inputs)
foreach i in $wiotcountry {
	foreach j in $wiotsector {	
		capture: by year wiot12_jp: egen summed_import`i'`j' = sum(import`i'`j')
		capture: gen summed_import`i'`j'=0
		capture: by year wiot12_jp: gen sector_gamma_import`i'`j' = summed_import`i'`j'/summed_inputs
		gen weight`i'`j' = sector_gamma_import`i'`j' / WIOT`i'`j'
		replace gamma_`i'`j' = gamma_`i'`j' / weight`i'`j'
		replace gamma_`i'`j' = 0 if gamma_`i'`j'==.
		replace FRA_Residual = FRA_Residual - gamma_`i'`j'
	}
}
gen To_Drop = 0
replace To_Drop = 1 if FRA_Residual<0
keep siren year wiot12_jp To_Drop
save To_Drop_${sample}_`y', replace
restore

merge 1:1 siren year wiot12_jp using To_Drop_${sample}_`y', nogenerate
drop if To_Drop==1
erase To_Drop_${sample}_`y'.dta

foreach i in $wiotcountry {
	foreach j in $wiotsector {
		capture: replace import`i'`j'=0 if import`i'`j'==.
		gen gamma_`i'`j' =.
		capture: replace gamma_`i'`j' = import`i'`j' / firm_inputs
		replace gamma_`i'`j'=0 if gamma_`i'`j'==. | import_share==0
		gen pre_gamma_`i'`j' = gamma_`i'`j'
	}
}
foreach i in $wiotcountry {
	foreach j in $wiotsector_NT_Only {
		gen gamma_`i'`j'=0
		gen pre_gamma_`i'`j' = gamma_`i'`j'
	}
}

gen FRA_Residual=1
bysort year wiot12_jp: egen summed_inputs`i'`j' = sum(firm_inputs)
foreach i in $wiotcountry {
	foreach j in $wiotsector {	
		capture: by year wiot12_jp: egen summed_import`i'`j' = sum(import`i'`j')
		capture: gen summed_import`i'`j'=0
		capture: by year wiot12_jp: gen sector_gamma_import`i'`j' = summed_import`i'`j'/summed_inputs
		gen weight`i'`j' = sector_gamma_import`i'`j' / WIOT`i'`j'
		replace gamma_`i'`j' = gamma_`i'`j' / weight`i'`j'
		replace gamma_`i'`j' = 0 if gamma_`i'`j'==.
		replace FRA_Residual = FRA_Residual - gamma_`i'`j'
		drop summed_import`i'`j' sector_gamma_import`i'`j' weight`i'`j'
	}
}

drop if FRA_Residual<0
gen WIOT_FRA_Residual=1
foreach i in $wiotcountry {
	foreach j in $wiotsector {
		replace WIOT_FRA_Residual = WIOT_FRA_Residual - WIOT`i'`j'
	}
}
gen weight_FRA = WIOT_FRA_Residual/FRA_Residual
replace weight_FRA=0 if weight_FRA==.
foreach i in $wiotsector_ALLXJP {
	gen gamma_FRA`i'= WIOTFRA`i' / weight_FRA
	replace gamma_FRA`i'= 0 if gamma_FRA`i'==.
}
gen sum_gammas = 0
foreach i in $wiotcountry {
	foreach j in $wiotsector {
		replace sum_gammas = sum_gammas + gamma_`i'`j'
	}
} 
foreach i in $wiotsector_ALLXJP {
	replace sum_gammas = sum_gammas + gamma_FRA`i'
}

sum sum_gammas, d

drop import* gamma_TR_domestic sect logit_labor_share gamma_NT gamma_TR gamma_TR_domestic


*** +++ Calculate Homogeneous Gamma Method 2 (solely for year=$year+++ ***

if "`y'"=="$year"{
	bys year wiot12_jp: egen tot_inputs = sum(firm_inputs)
	gen homo_FRA_Residual = 1
	foreach i in $wiotcountry {
		foreach j in $wiotsector_ALLXJP {
			gen weighted_gamma_`i'`j'=gamma_`i'`j'*(firm_inputs/tot_inputs)
			bys year wiot12_jp: egen wiot_gamma_`i'`j'= sum(weighted_gamma_`i'`j')
			gen holder`i'`j' = (firm_inputs/tot_inputs) if gamma_`i'`j'==0
			replace holder`i'`j' = 0 if gamma_`i'`j'>0
			bys year wiot12_jp: egen non_traders_weight`i'`j' = sum(holder`i'`j')
			bys year wiot12_jp: gen homo_gamma_`i'`j' = wiot_gamma_`i'`j' / (1-non_traders_weight`i'`j') if gamma_`i'`j'>0
			replace homo_gamma_`i'`j'=0 if gamma_`i'`j'==0 
			replace homo_FRA_Residual = homo_FRA_Residual - homo_gamma_`i'`j'
			drop weighted_gamma_`i'`j' holder`i'`j' non_traders_weight`i'`j' wiot_gamma_`i'`j'
		}
	}
	drop if homo_FRA_Residual<0
	gen weight_FRA_1 = homo_FRA_Residual/WIOT_FRA_Residual
	replace weight_FRA_1=0 if weight_FRA_1==.
	foreach i in $wiotsector_ALLXJP {
		gen homo_gamma_FRA`i'= WIOTFRA`i' * weight_FRA_1
		replace homo_gamma_FRA`i'= 0 if homo_gamma_FRA`i'==.
	}
	gen sum_homo_gammas = 0
	foreach i in $wiotcountry_FRA {
		foreach j in $wiotsector_ALLXJP {
			replace sum_homo_gammas = sum_homo_gammas + homo_gamma_`i'`j'
		}
	} 
	sum sum_homo_gammas, d
	drop homo_FRA_Residual
}

*** +++ Calculate Pi +++ ***

merge 1:1 siren year wiot12_jp using $outputpath/bilateralexports_wide_${sample}_`y', nogenerate
drop if firm_import==.

gen firm_export=0
foreach i in $wiotcountry {
	replace firm_export = firm_export + export`i'
}

foreach i in $wiotcountry {
	replace export`i'=0 if export`i'==.
	bys year wiot12_jp: egen tot`i'=sum(export`i')
	gen pi_nkjpt`i'=export`i'/tot`i'
}
foreach i in $wiotcountry {
	replace pi_nkjpt`i'=0 if pi_nkjpt`i'==.
}
foreach i in $wiotcountry {
	foreach j in $wiotsector_NT_Only {
		replace pi_nkjpt`i'=0 if wiot12_jp=="`j'"
	}
}
bys year wiot12_jp: egen tot=sum(cafranc)
gen pi_nkjptFRA=cafranc/tot

foreach i in $wiotcountry_FRA {
	bys year wiot12_jp: egen homo_pi_`i' = nvals(siren) if pi_nkjpt`i'>0
	replace homo_pi_`i' = 1/homo_pi_`i' 
	replace homo_pi_`i'=0 if homo_pi_`i'==.
}


*** +++ Save Master Data +++ ***


keep siren year wiot12_jp firm_import firm_import_old firm_inputs vabcf gr_vabcf gr_saltrai gr_firm_inputs gr_firm_import catotal cafranc firm_export labor_share pre_gamma* gamma_* pi* homo* win_*
order siren year wiot12_jp firm_import firm_import_old firm_inputs vabcf gr_vabcf gr_saltrai gr_firm_inputs gr_firm_import  catotal cafranc firm_export labor_share pre_gamma* gamma_* pi* homo* win_*
erase Calibration_Hold_${sample}_`y'.dta
compress
saveold "$outputpath/Firm_Level_pi_alpha_gamma_${method}_${sample}_`y'", replace version(13)
}


erase temp___.dta

